package com.ccz.demo.bookdemo.javayibubianchengshizhan.c2;

import com.ccz.demo.ConfirmStates;
import com.ccz.demo.PriceCategoryEnum;

import java.util.concurrent.*;

/**
 * @ClassName Example2_2
 * @Author caihaoyun
 * @Date 2020/5/26
 * @Version V1.0
 **/
public class Example2_2 {
    // 0.添加自定义线程池
    private final static int AVALIABLE_PROCESSORS = Runtime.getRuntime().availableProcessors();
    private final static ThreadPoolExecutor POOL_EXECUTOR = new ThreadPoolExecutor(AVALIABLE_PROCESSORS,
            AVALIABLE_PROCESSORS * 2, 1, TimeUnit.MINUTES, new LinkedBlockingQueue<>(5),
            new ThreadPoolExecutor.CallerRunsPolicy());

    public static void main(String[] args) throws InterruptedException, ExecutionException {
        long start = System.currentTimeMillis();
        // 1.开启异步单元执行任务A
        POOL_EXECUTOR.execute(() -> DoSomething.doSomethingA());


        POOL_EXECUTOR.execute(() -> DoSomething.doSomethingB());

        // 2.执行任务B
        //DoSomething.doSomethingB();

        Future<String> result = POOL_EXECUTOR.submit(() -> DoSomething.doSomethingC());
        System.out.println(result.get());

        // 3.同步等待线程A运行结束
        System.out.println(System.currentTimeMillis() - start);

        System.out.println(ConfirmStates.CANCEL.toString());
        System.out.println(PriceCategoryEnum.CATEGORY_ADULTS.toString());
        Thread.currentThread().join();
    }
}
